package com.hongxun.budget.ui.activity.sg;

import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.hongxun.budget.R;
import com.hongxun.budget.ui.activity.BaseActivity;
import com.hongxun.budget.ui.fragment.BaseFragment;
import com.hongxun.budget.ui.fragment.sg.MaterialPresent;
import com.hongxun.budget.ui.fragment.sg.MiddleCheck;
import com.hongxun.budget.ui.fragment.sg.ProjectProgress;
import com.hongxun.budget.utils.NetUtils;
import com.hongxun.budget.utils.SgStrKey;

import butterknife.BindView;

/**
 * 施工项目
 * 项目进度1级界面
 * 材料到场1级界面
 * 中间检查1级界面
 */
public class ConstructionActivity extends BaseActivity {
    @BindView(R.id.right_btn)
    Button rightBtn;
    @BindView(R.id.center_title)
    TextView centerTitle;
    private String tabName;

    private BaseFragment baseFragment;
    private ProjectProgress progressFragment;
    private MaterialPresent presentFragment;
    private MiddleCheck checkFragment;

    @Override
    public int getLayoutId() {
        return R.layout.sg_activity_flayout;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        rightBtn.setVisibility(View.VISIBLE);
        rightBtn.setText("服务器");
        rightBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getOnLineProJect();
            }
        });

        tabName = getIntent().getExtras().getString("tabName");
        centerTitle.setText(tabName);

        changeTab();
    }

    private void getOnLineProJect() {
        if (NetUtils.isConnected(context)) {
            startActivity(new Intent(activity, AuxiliaryActivity.class)
                    .putExtra("tabName", SgStrKey.sgxmlb));
        } else {
            showMsg("没有网络哟^_^");
        }
    }


    private void changeTab() {
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        if (baseFragment != null) transaction.hide(baseFragment);
        switch (tabName) {
            case SgStrKey.xmjd:
                if (progressFragment == null) {
                    progressFragment = new ProjectProgress();
                    transaction.add(R.id.flayout1, progressFragment, tabName).commit();
                } else {
                    transaction.show(progressFragment).commit();
                    progressFragment.onRefresh();
                }
                baseFragment = progressFragment;
                break;
            case SgStrKey.cldc:
                if (presentFragment == null) {
                    presentFragment = new MaterialPresent();
                    transaction.add(R.id.flayout1, presentFragment, tabName).commit();
                } else {
                    transaction.show(presentFragment).commit();
                    presentFragment.onRefresh();
                }
                baseFragment = presentFragment;
                break;
            case SgStrKey.zjjc:
                if (checkFragment == null) {
                    checkFragment = new MiddleCheck();
                    transaction.add(R.id.flayout1, checkFragment, tabName).commit();
                } else {
                    transaction.show(checkFragment).commit();
                    checkFragment.onRefresh();
                }
                baseFragment = checkFragment;
                break;
        }
    }

}
